const express = require('express')
const router = express.Router()
const db = require('../../connection/Connection')
const jwt = require('jsonwebtoken')
const moment = require('moment')
const SECRET = 'asdfta219*&%^jWJSHFJ'


router.post('/addTrain', async (req, res) => {
    console.log(req.body)
    //token
    const result = jwt.verify(req.body.token, SECRET)
    let sql = 'select departmentId from user_role where username = ?'
    db.base(sql, result.username, response_temp => {
        console.log(req.body)
        req.body.trainStart = moment(req.body.trainStart).format('YYYY-MM-DD HH:mm:ss')
        req.body.trainEnd = moment(req.body.trainEnd).format('YYYY-MM-DD HH:mm:ss')
        console.log(req.body)
        if (response_temp != null && response_temp.length == 1) {
            sql = 'select workNumber from department_employees WHERE departmentId = ?'
            db.base(sql, response_temp[0].departmentId, response_temp => {
                console.log(response_temp)
                flag = false
                for (let i = 0; i < response_temp.length; i++) {
                    if (req.body.workNumber == response_temp[i].workNumber) {
                        flag = true
                    }
                }
                if (flag) {
                    sql = 'SELECT train_id FROM employees WHERE work_number = ?'
                    db.base(sql, req.body.workNumber, response_temp => {
                        if (response_temp != null && response_temp.length == 1) {
                            console.log(req.body)
                            sql = 'UPDATE train SET train_type = ?,train_site = ?,train_start = ?,train_end= ?,train_hour=?,train_content=?,train_score=? WHERE train_id = ?'
                            let data = [
                                req.body.trainType,
                                req.body.trainSite,
                                req.body.trainStart,
                                req.body.trainEnd,
                                req.body.trainHour,
                                req.body.trainContent,
                                req.body.trainScore,
                                response_temp[0].train_id
                            ]
                            db.base(sql, data, response_temp => {
                                if (response_temp != null && response_temp.affectedRows == 1) {
                                    res.json({
                                        result: "success"
                                    })
                                } else {
                                    res.json({
                                        result: "error"
                                    })
                                }
                            })
                        } else {
                            res.json({
                                result: "error"
                            })
                        }
                    })
                } else {
                    res.json({
                        result: "error"
                    })
                }
            })
        } else {
            res.json({
                result: "error"
            })
        }
    });
})

module.exports = router
